﻿using Emgu.CV.CvEnum;
using OpenCvApplication.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace OpenCvApplication.View.CommonView
{
    public partial class BoxFilterParameterControl : UserControl
    {
        public BoxFilterParameterControl()
        {
            InitializeComponent();


            BindDepthType();

            foreach (Control item in this.Controls)
            {
                if (item is TrackBar trackBar)
                    trackBar.Scroll += trackBar_Scroll;
            }
        }
        private void trackBar_Scroll(object sender, EventArgs e)
        {
            RefreshShow();
            ValueChange?.Invoke(this, Args);
        }
        public Action<object, BoxFilterEventArgs> ValueChange { get; set; }
        private void BindDepthType()
        {
            var enumValues = Enum.GetValues(typeof(DepthType));
            var list = new List<IdValues>();
            foreach (Enum value in enumValues)
            {
                int hc = value.GetHashCode();
                list.Add(new IdValues
                {
                    Id = hc.ToString(),
                    Value = value.ToString(),
                    Standby = hc
                });
            }

            this.cmbBorderType.ValueMember = "Id";
            this.cmbBorderType.DisplayMember = "Value";
            this.cmbBorderType.DataSource = list;
            this.cmbBorderType.SelectedIndex = 0;
        }
        private void RefreshShow()
        {
            lblSizeWidth.Text = SizeWidth.ToString();
            lblSizeHeight.Text = SizeHeight.ToString();
            lblSigmaX.Text = X.ToString();
            lblsigmaY.Text = Y.ToString();
        }
        private void cbPoint_CheckedChanged(object sender, EventArgs e)
        {
            CbPoint(!cbPoint.Checked);
            RefreshShow();
        }
        private void CbPoint(bool enabled)
        {
            tcbSigmaX.Enabled = tcbsigmaY.Enabled = enabled;
        }

        private void BoxFilterParameterControl_Load(object sender, EventArgs e)
        {
            ValueChange?.Invoke(this, Args);
        }

        private void checkBox3_CheckedChanged(object sender, EventArgs e)
        {
            cmbBorderType.Enabled=!checkBox3.Checked;
        }

        public int SizeWidth
        {
            get => tcbSizeWidth.Value;
        }
        public int SizeHeight
        {
            get => tcbSizeHeight.Value;
        }
        public int X
        {
            get => cbPoint.Checked ? -1 : tcbSigmaX.Value;
        }
        public int Y
        {
            get => cbPoint.Checked ? -1 : tcbsigmaY.Value;
        }
        public DepthType Depth
        {
            get => (DepthType)(cmbBorderType.Items[cmbBorderType.SelectedIndex] as IdValues).Standby;
        }
        public BoxFilterEventArgs Args
        {
            get
            {
                var result = new BoxFilterEventArgs(SizeWidth, SizeHeight, X, Y, Depth);
                result.IsUseOriginalDepth = checkBox3.Checked;
                return result;
            }
        }
    }
    public class BoxFilterEventArgs : EventArgs
    {
        public BoxFilterEventArgs()
        {
            IsUseOriginalDepth = false;
        }
        public BoxFilterEventArgs(int sizeWidth, int sizeHeight, int x, int y, DepthType depth) : this()
        {
            this.SizeWidth = sizeWidth;
            this.SizeHeight = sizeHeight;
            this.X = x;
            this.Y = y;
            this.Depth = depth;
        }
        public int SizeWidth { get; set; }
        public int SizeHeight { get; set; }
        public int X { get; set; }
        public int Y { get; set; }
        public bool IsUseOriginalDepth { get; set; }
        public DepthType Depth { get; set; }
    }
}
